//
//  AppDelegate.m
//  Nooryes
//
//  Created by baken on 2020/2/5.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "AppDelegate.h"
#import "NYTabBarController.h"
#import "NYAcccountViewModel.h"
#import <MOBFoundation/MobSDK+Privacy.h>
#import <ShareSDK/ShareSDK.h>
#import "NYRecommendViewController.h"
#import "NYArticleDetailViewController.h"
#import "NYNavigationViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NYAcccountViewModel *accountVM = [NYAcccountViewModel new];
    [MobSDK uploadPrivacyPermissionStatus:NO onResult:^(BOOL success) {
        
    }];
    [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
        //QQ https://d7hxd.share2dlink.com/qq_conn/101864966
        [platformsRegister setupQQWithAppId:TencentAppKey appkey:TencentAppSecret];
        
        [platformsRegister setupWeChatWithAppId:WeChatAppId appSecret:WeChatAppSecret universalLink:@"https://d7hxd.share2dlink.com/"];
        
    }];
    [accountVM getUserInfoWithCompletionHandle:^(NSError *error) {
        if (!error) {
            //            self.account = accountVM.account;
        }
    }];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    NYTabBarController *customeBar = [[NYTabBarController alloc] init];
    self.tabbarController = customeBar;
    self.window.rootViewController =customeBar;
    //    self.tabbarController = customeBar;
    [self.window makeKeyAndVisible];
    
    return YES;
}

//ios 12 分享返回
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    //    nooryes://articleid=xx
    NSString *abString = url.absoluteString;
    NSArray *array = [abString componentsSeparatedByString:@"="];
    
    if (array.count ==2) {
        if ([array[0] containsString:@"nooryes"]) {
            // 1.获取导航栏控制器
            NYTabBarController *nytab = (NYTabBarController *)self.window.rootViewController;
            // 2.获得主控制器
            UINavigationController *rootNav = (UINavigationController *)[nytab.childViewControllers firstObject];
            UIViewController *vc = [rootNav.childViewControllers firstObject];
            if ([vc isKindOfClass:[NYRecommendViewController class]]) {
                NYRecommendViewController * recomVC = (NYRecommendViewController *)vc;
                // 3.每次跳转前必须是在跟控制器(细节)
                //        [rootNav popViewControllerAnimated:NO];
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RecommendStoryboard" bundle:nil];
                NYArticleDetailViewController *detailVC =[storyboard  instantiateViewControllerWithIdentifier:@"NYArticleDetailViewController"];
                detailVC.articleID = array[1];
                [recomVC.navigationController pushViewController:detailVC animated:YES];
            }
        }
        
    }
    return YES;
    
}

//ios 13 分享返回
- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity API_AVAILABLE(ios(13.0)){
    
}

@end
